Type.registerNamespace("BM");

// ListControlValueHandler object

BM.ListControlValueHandler = function(leafItem) {
    BM.ListControlValueHandler.initializeBase(this, [leafItem]);
}

BM.ListControlValueHandler.prototype = {
    getControlValue: function() {
        var joinChar = this.getValueSeparator() != null ? this.getValueSeparator() : '';
        return this.getValuesArray().join(joinChar);   
    },

    setControlValue: function(value) {
        var values = value.split(this.getValueSeparator());        
        var select = this.getInput();
        for (var i = 0; i < select.options.length; i++) {
            var option = select.options[i];
            if (Array.contains(values, option.value)) {                
                option.selected = true;
            } else {
                option.selected = false;
            }
        }        
    },
    
    getValuesArray: function() {
        var array = new Array();  
        var select = this.getInput();           
        for(var i = 0; i < select.options.length; i++) {
            var option = select.options[i];
            if (option.selected && option.value != null && option.value != '') {                                
                array[array.length] = option.value;
            }
        }
        return array;
    },
    
    getValueSeparator: function() {
        return this.getInput().getAttribute('valueSeparator');        
    }
}

BM.ValueHandler['ListBoxValueHandler'] = BM.ListControlValueHandler;
BM.ValueHandler['DropDownListValueHandler'] = BM.ListControlValueHandler;
BM.ListControlValueHandler.registerClass("BM.ListControlValueHandler", BM.ValueHandler);